package com.netty.test;

import java.net.InetSocketAddress;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NettyServer {
	
	public static void main(String[] args) {
		NettyServer server = new NettyServer();
	
		server.start();
	}
	
	public void start() {
		ServerHandler handler = new ServerHandler();
		EventLoopGroup group = new NioEventLoopGroup();
		try {

			ServerBootstrap b = new ServerBootstrap();
		
			b.group(group)
			.channel(NioServerSocketChannel.class)
			.localAddress(new InetSocketAddress(5858))
			.childHandler(new ChannelInitializer<SocketChannel>() {

				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(handler);
					
				}
				
			});
			
			ChannelFuture f = b.bind().sync();
			f.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			
		}finally {
			try {
				group.shutdownGracefully().sync();
			} catch (InterruptedException e) {
				
			}
		}
		
	}

}
